################################################################################
#   Copyright 2011 Shane C. Mason & Wrongdog.net                               
#
#   Licensed under the Apache License, Version 2.0 (the "License"); 
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
################################################################################
import shelve

class WrongConfig:
    """Basic configuration class, uses shelve to persist.
    NOTE: This is a borg pattern, so all instances will be using the same config file!"""
    
    __shared_state = {}
    
    def __init__(self, path=None):
        """This is a borg pattern, so all instances will be using the same config file!"""
        self.__dict__ = self.__shared_state
        #otherwise, assume it is already installed
        if( path != None ):
            self.__conf = shelve.open( path);
            self.__path = path
        
    def removeValue( self, key ):
        """Remove the given value. Returns true if key exists and is deleted, false otherwise."""
        if self.__conf.has_key( key ):
            del self.__conf[key]
            return True
        else:
            return False
    
    def getValue( self, key ):
        """Gets the value for key - returns None if key does not exist"""
        if self.__conf.has_key( key ):
            return self.__conf[key]
        else:
            return None
        
    def setValue( self, key, value ):
        """Set the value os the given key"""
        self.__conf[key] = value
        return True